import gcoord from "../../miniprogram_npm/gcoord/dist/gcoord";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    shortName: '',
    corpCredit: '',
    agencyName: '',
    corpName: '',
    position: '',
    backNum: '',
    // 问卷的答案
    answers: {
      q1: '',
      q2: "",
      q3: "",
      q4: "",
      q5_a: "",
      q5_b: "",
      q5_c: "",
      q5_d: "",
      q5_e: "",
      q5_f: "",
      q6: "",
      q7_a: "",
      q7_b: "",
      q7_c: "",
      q7_d: "",
      q7_e: "",
      q7_f: "",
      q8_1: "",
      q8_2: "",
      q9_1: "",
      q9_2: "",
      q9_3: "",
      q9_4: "",
      q9_5: "",
      q9_6: "",
    },
    onlyView: false,
    laborFlag:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    console.log(options,'再给我传')
    const data = wx.getStorageSync('punchObj')
    this.setData({
      shortName: data.shortName,
      corpCredit: data.corpCredit,
      agencyName: data.agencyName,
      corpName: data.corpName,
      position: data.corpLocation,
      backNum: options.backNum,
      onlyView: options.onlyView == '1' ? false : true,
      laborFlag:options.laborFlag?options.laborFlag:''
    })
    if (data.questionnaire) {
      this.setData({
        answers: data.questionnaire
      })
    }
  },
  onChange (e) {
    const key = e.currentTarget.dataset.key
    const item = e.currentTarget.dataset.item
    if (this.data.answers[key] === item) {
      this.data.answers[key] = ''
    } else {
      this.data.answers[key] = item
    }
    this.setData({
      answers: this.data.answers
    })
  },
  bindKeyInput (e) {
    const key = e.currentTarget.dataset.key
    const value = e.detail.value
    this.data.answers[key] = value.replace(/\s+/g, '')
    this.setData({
      answers: this.data.answers
    })
  },
  async goSub () {
    const answers = Object.values(this.data.answers)
    const isHas = answers.some(val => val)
    if (!isHas) {
      wx.showToast({
        title: '请填写问卷后提交',
        icon: 'none'
      })
      return
    }
    const userInfo = wx.getStorageSync('userInfo')
    const params = {
      userId: userInfo.userId,
      corpCredit: this.data.corpCredit,
      questionnaire: this.data.answers,
      questionnaireStatus: '问卷已完成',
      laborFlag:this.data.laborFlag
    }
    console.log(params,'paramsparams')
    const res = await getApp().globalData.request(
      "/visit/updateQuestionnaire",
      { ...params },
      "POST"
    );
    console.log(res, 'res')
    if (res.code == 200) {
      wx.navigateTo({
        url: `/pages/commitsucceed/commitsucceed?backNum=${this.data.backNum}&text=${"问卷已提交成功"}`,
      })
    }else{
      // wx.showToast({
      //   title: res.,
      //   icon: 'none'
      // })
    }
  },
  onChangeMore (e) {
    const value = e.currentTarget.dataset.value
    const key = e.currentTarget.dataset.key
    if (this.data.answers[key]) {
      this.data.answers[key] = ''
    } else {
      this.data.answers[key] = value
    }
    this.setData({
      answers: this.data.answers
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () { },
});
